package com.github.live.manager;

import com.github.livebase.bean.Channel;
import com.github.livebase.bean.EpgInformation;
import com.github.livebase.database.EpgOperator;
import com.github.livebase.event.Event;
import com.github.livebase.event.EventBus;
import com.github.livebase.event.EventListener;
import com.github.livebase.event.RegistrableListener;
import com.github.livebase.event.SimpleEventBus;
import com.github.livebase.util.CastTime;
import com.github.livebase.util.LiveContext;
import com.github.livebase.util.base.CollectionsUtil;
import com.github.livebase.util.base.DateUtil;
import com.github.livebase.util.base.ObjectsUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName EpgInformationManager
 * @description: epg信息管理器
 * @author: biao532
 * @create: 2023-12-20 13:41
 * @Version 1.0
 **/
public class EpgManager extends RegistrableListener implements EventListener {
    // 当前信息 缓存
    private EpgInformation currentEpgInfo;
    // 下一个信息 缓存
    private EpgInformation nextEpgInfo;
    // 当前时间
    private long currentTime;
    private EpgOperator epgOperator;
    protected EpgManager(LiveContext context) {
        super(EventBus.EVENT_CHANGE_TIME, true);
        this.epgOperator = context.getLiveDatabaseHelper().getEpg();
        currentTime = System.currentTimeMillis();
    }

    /**
     * 获取指定频道的当前epg信息
     * @param channel
     * @return
     */
    public EpgInformation getCurrentEpgInfo(Channel channel) {
        if (currentEpgInfo != null
                && channel.getId().equals(currentEpgInfo.getChannelId())
                && DateUtil.betweenAnd(currentTime, currentEpgInfo.getStart(), currentEpgInfo.getEnd())) {
            return currentEpgInfo;
        }

        EpgInformation e = epgOperator.selectCurrent(channel.getId(), currentTime);
        currentEpgInfo = e;
        return ObjectsUtil.getOrDefault(e, EpgInformation.NO_INFORMATION);
    }

    /**
     * 获取指定频道的下一个epg信息
     * @param channel
     * @return
     */
    public EpgInformation getNextEpgInfo(Channel channel) {
        if (nextEpgInfo != null
                && channel.getId().equals(nextEpgInfo.getChannelId())
                && DateUtil.betweenAnd(currentTime, nextEpgInfo.getStart(), nextEpgInfo.getEnd())) {
            return nextEpgInfo;
        }

        CastTime.start("getNextEpgInfo");
        EpgInformation e = epgOperator.selectNext(channel.getId(), currentTime);
        nextEpgInfo = e;
        CastTime.end("getNextEpgInfo");
        return ObjectsUtil.getOrDefault(e, EpgInformation.NO_INFORMATION);
    }

    /**
     * 获取指定频道指定天的epg信息
     * @param channel
     * @param day
     * @return
     */
    public List<EpgInformation> getEpgInfosByDay(Channel channel, int day) {
        if (channel == null) {
            return Arrays.asList(EpgInformation.NO_INFORMATION);
        }
        List<EpgInformation> l = epgOperator.selectList(channel.getId(), day);
        if (CollectionsUtil.isEmpty(l)) {
            l = Arrays.asList(EpgInformation.NO_INFORMATION);
        }
        return l;
    }

    /**
     * 获取指定频道指定时间的epg信息
     * @param channel
     * @param date
     * @return
     */
    public EpgInformation getEpgInfoByDate(Channel channel, Date date) {
        EpgInformation e = epgOperator.selectCurrent(channel.getId(), date.getTime());
        return ObjectsUtil.getOrDefault(e, EpgInformation.NO_INFORMATION);
    }

    @Override
    public void onEvent(Event event) {
        currentTime = System.currentTimeMillis();
    }

    public void close() {
        EventBus bus = SimpleEventBus.getDefault();
        bus.unregister(EventBus.EVENT_CHANGE_TIME, this);
    }
}
